<?php
$root = $_SERVER['DOCUMENT_ROOT'] . "/php/";

include_once "$root/class/SqlConnect.php";
include_once "$root/class/SqlManager.php";

class UserManager
{
    private $currentId = 0;
    private $sqlManager;
    public static $MEMBER = 1;
    public static $UNIVERSAL = 2;
    public static $ADMIN = 3;

    /**
     * 初始化。
     * @param int $currentId
     */

    public function __construct($currentId = 000)
    {
        $this->currentId = $currentId;
        $sqlConnect = new SqlConnect();
        $conn = $sqlConnect->connect();
        $sqlManager = new SqlManager("user");
        $sqlManager->setSqlConnect($conn);
        $this->sqlManager = $sqlManager;
    }

    private function removeNotUseData(&$arr)
    {
        unset($arr["id"]);
    }

    private function parseArray($arr)
    {
        foreach ($arr as $k => $v) {
            if (is_array($v)) {
                $arr[$k] = json_encode($v);
            }
        }
        return $arr;
    }

    public function listUser()
    {
        return $this->sqlManager->getTableData();
    }

    public function putUserData(User $user)
    {
        $arr = json_decode($user->getJson(), true);
        $this->removeNotUseData($arr);
        return $this->sqlManager->addFieldData($arr);
    }


    /**
     * 获取当前操作的用户ID。
     * @return int
     */

    public function getCurrentId()
    {
        return $this->currentId;
    }

    public function setCurrentId($currentId)
    {
        $this->currentId = $currentId;
    }

    public function getSqlManager()
    {
        return $this->sqlManager;
    }

    public function addUser($arr)
    {
        $user = new User();
        $user->write($arr);
        return $this->putUserData($user);
    }

    public function removeUser()
    {
        return $this->sqlManager->deleteField($this->currentId);
    }

    public function queryUser($where)
    {
        return $this->sqlManager->queryFieldData($where, false);
    }

    public function alterUser($where, $strArr)
    {
        return $this->sqlManager->alterData($where, $strArr);
    }

    public function getUser()
    {
        return $this->sqlManager->queryFieldData($this->currentId);
    }
}